package com.monna.games_2048;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.monna.service.MusicService;

/**
 * 开始游戏、退出登录操作处理
 *
 * @author 廖梦青
 */
public class IndexActivity extends AppCompatActivity {

    private Button startGames;
    private ImageView imgMusic;
    private String isBgMusic = "true";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        /**
         * 单击按钮播放背景音乐，再次单机停止播放背景音乐
         */
        imgMusic = findViewById(R.id.img_music);
        imgMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bgMusic();
            }
        });

        startGames = findViewById(R.id.games_start);

        /**
         * 开始游戏
         */
        startGames.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IndexActivity.this, MainActivity.class);
                intent.putExtra("isBgMusic", isBgMusic);
                startActivity(intent);
            }
        });

        /**
         * 退出登陆
         */
        Button signOut = findViewById(R.id.sign_out);
        signOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                singnOut();
            }
        });
    }

    /**
     * 退出登陆
     */
    private void singnOut(){
        Intent intent = new Intent(IndexActivity.this, LoginActivity.class);
        /**
         * 清空登陆信息
         */
        SharedPreferences preferences = getSharedPreferences("userInfo", IndexActivity.MODE_PRIVATE);
        preferences.edit().clear().commit();

        /**
         * 关闭背景音乐
         */
        stopService(new Intent(IndexActivity.this, MusicService.class));
        finish();
        startActivity(intent);
    }

    /**
     * 背景音乐
     */
    public void bgMusic(){
        if (!MusicService.isPlay){
            //启动服务，播放背景音乐
            startService(new Intent(IndexActivity.this, MusicService.class));
            isBgMusic = "true";
            //更换背景音乐图标
            imgMusic.setImageDrawable(getResources().getDrawable(R.mipmap.ic_music, null));
        }else {
            //停止服务，停止播放背景音乐
            stopService(new Intent(IndexActivity.this, MusicService.class));
            isBgMusic = "false";
            //更换停止背景音乐图标
            imgMusic.setImageDrawable(getResources().getDrawable(R.mipmap.ic_music_invalid, null));
        }
    }

    /**
     * 进入界面时启动背景音乐
     */
    @Override
    protected void onStart(){
        super.onStart();
        if (isBgMusic.equals("true")){
            startService(new Intent(IndexActivity.this, MusicService.class));
        }else {
            stopService(new Intent(IndexActivity.this, MusicService.class));
        }
    }

    /**
     * 按下返回键后停止背景音乐
     * @param keyCode
     * @param event
     * @return
     */
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if (keyCode == KeyEvent.KEYCODE_BACK){
            stopService(new Intent(IndexActivity.this, MusicService.class));
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
